#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="ChasingCameraController.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Render.
//
//   starLiGHT.Render is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Render. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Render are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Camera.TwoD
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;

    #endregion

    public class ChasingCameraController : ICameraController
    {
#region MemberVariables
        private ITrackable chasingObject;
        private bool elasticCamera;
        private Vector2 cameraTarget;
        private float steps = 25.0f;
        private float maxVelocity = 2.5f;
#endregion

#region Constructors
        public ChasingCameraController()
        {
        }

        public ChasingCameraController(ITrackable chasingObject)
        {
            this.chasingObject = chasingObject;
        }

        public ChasingCameraController(bool elasticity)
        {
            this.elasticCamera = elasticity;
        }

        public ChasingCameraController(ITrackable chasingObject, bool elasticity)
        {
            this.chasingObject = chasingObject;
            this.elasticCamera = elasticity;
        }
#endregion

        public ITrackable ChasingObject
        {
            get { return this.chasingObject; }
            set { this.chasingObject = value; }
        }

        public string Name
        {
            get { return "Chasing"; }
        }

        public bool Elasticity
        {
            get { return this.elasticCamera; }
            set { this.elasticCamera = value; }
        }

        public float Steps
        {
            get { return this.steps; }
            set { this.steps = value; }
        }

        public float MaxVelocity
        {
            get { return this.maxVelocity; }
            set { this.maxVelocity = value; }
        }

        public void Update(GameTime gameTime, Camera2D camera)
        {
            if (!this.elasticCamera && this.chasingObject != null)
            {
                camera.Anchor = this.chasingObject.Center;
            }
            else if (this.chasingObject != null)
            {
                if (this.cameraTarget != this.chasingObject.Center)
                {
                    this.cameraTarget = this.chasingObject.Center;
                }

                Vector2 des_vel = this.cameraTarget - camera.Anchor;
                float distance = des_vel.Length();
                float speed = distance / this.steps;
                if (speed > this.maxVelocity)
                {
                    speed = this.maxVelocity;
                }

                des_vel *= speed / distance;
                camera.Anchor += des_vel;
            }
        }
    }
}